package com.twu.biblioteca;


import java.io.IOException;
import java.io.PrintStream;
import java.util.HashMap;

//import static sun.org.mozilla.javascript.internal.ScriptRuntime.typeof;

public class BibliotecaApp {
    PrintStream printStream;
    static int userInput;
    Library library = new Library();
    Customer customer;

    public BibliotecaApp(PrintStream printStream){
        this.printStream = printStream;

    }

    public static void main(String[] args){
        BibliotecaApp bibliotecaApp = new BibliotecaApp(System.out);
        BibliotecaMenu bibliotecaMenu = new BibliotecaMenu(System.out, new HashMap<Integer, String>());
        bibliotecaApp.printWelcome();
        bibliotecaMenu.buildMenu();
        while(true){
            bibliotecaMenu.printMenu();
            bibliotecaApp.getInput(bibliotecaMenu);
            bibliotecaApp.performOption(userInput);
            if(userInput==5){
                System.out.println("Thank you for using Biblioteca. Good Bye :)");
                break;
            }
        }
    }

    public void printWelcome() {
        printStream.println("Welcome to the Biblioteca!");
    }

    public void getInput(BibliotecaMenu bibliotecaMenu){
        UserInputHandler userInputHandler = new UserInputHandler(System.in);

        do try {
            userInput = userInputHandler.getUserSelection();
            bibliotecaMenu.validateUserInput(userInput);
            break;
        } catch (IOException e) {
            printStream.println("Select a valid option!!!");
        }
            while(true);
    }

    public void performOption(int userInput){

        if ((userInput == 2 || userInput == 3) && customer == null)  {
            customer = new LogIn(new CustomerCollection()).requestAndAttemptLogIn();
            if (customer == null) return;
        }

        if(userInput==1) new ViewCommand(library).performAction();
        else if(userInput==2) new ReserveBookCommand(library).performAction();
        else if(userInput==3) new CheckMembershipDetails().performAction(customer);
        else if(userInput==4) new ViewMoviesCommand(library).performAction();

    }

}



